#include <iostream>
#include <vector>
using namespace std;

class Solution 
{
public:
    int totalFruit(vector<int>& fruits) 
    {
        int n = fruits.size();
        int hash[100001] = { 0 };
        int kinds = 0, ret = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            if(hash[fruits[right]]++ == 0) kinds++;
            
            while (kinds > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0) kinds--;
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret; 
    }
};

void Test01()
{
    vector<int> fruits = { 0,1,2,2 };
    Solution s;
    cout << s.totalFruit(fruits);
}


int main()
{
    Test01();

	return 0;
} 